package com.justrun.zjutsystem.zjutsystem.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/10/14
 */
public class ExcetListener<T> extends AnalysisEventListener<T> { //extends IService<T>

    private static final int BATCH_COUNT = 5;
    //    用作缓存池
    List<T> list = new ArrayList<>();

    private IService<T> baseService;

    public ExcetListener(IService<T> service) {
        this.baseService = service;
    }


    @Override
    public void invoke(T t, AnalysisContext analysisContext) {
        list.add(t);
        if (list.size() >= BATCH_COUNT) {
            baseService.saveBatch(list);
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        baseService.saveBatch(list);
        System.out.println("所有数据解析完成");
    }
}
